<?php

namespace App\Controller;

use Common\Controller\BaseController;
use App\Logic;

class GmoController extends BaseController
{
    private $surveyLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->surveyLogic = new Logic\SurveyLogic();
    }

    /**
     * GMO用户晚上数据访问地址
     *
     * @return void
     */
    public function result()
    {
        $data = I('param.');

        if (0 >= $data['point']) {
            echo 0;
            exit;
        }

        $logFileName = RUNTIME_PATH.'Logs/gmo/'.date('Y-m-d').'.log';

        $checkRes = $this->surveyLogic->checkHash($data);
        if (static::$res->isErr($checkRes)) {
            logs(array(
               'msg' => 'hash校验失败',
                'data' => $data,
                'res' => $checkRes,
            ), $logFileName);

            echo 1;
            exit;
        }

        $userId = $data['member_id'];

        $joinLogic = new Logic\JoinLogic();

        /**
         * 验证是否重复提交.
         */
        $infoRes = $joinLogic->findJoinInfo(array(
            'user_id' => $data['member_id'],
            'other_id' => $data['survey_id'],
            'state' => 3,
        ));
        $info = $infoRes['Result'];

        if (!empty($info)) {
            if ($info['other_info'] != $data['grant_times']) {
                echo 0;
                exit;
            }

            logs(array(
                'msg' => '重复提交',
                'data' => $data,
            ), $logFileName);

            echo 3;
            exit;
        }

        /**
         * 保存到参与表,并设置为审核通过.
         */
        $joinRes = $joinLogic->joinGmoTask($data);
        if (static::$res->isErr($joinRes)) {
            logs(array(
                'msg' => '调用joinGmoTask报错',
                'data' => $data,
                'res' => $joinRes,
            ), $logFileName);

            echo 2;
            exit;
        }

        echo 0;
        exit;
    }
}
